﻿Imports System.Data.OleDb

Public Class _VideosPlayComments
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sql3 As String = "INSERT INTO Video_Views_Stats (VideoID,DateViewed,TimeViewed) VALUES (@f1,@f2,@f3)"
        Dim conn3 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        conn3.Open()
        Dim cmd3 As OleDbCommand = New OleDbCommand(sql3, conn3)
        cmd3.Parameters.AddWithValue("@f1", Request.Params("id"))
        cmd3.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
        cmd3.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))
        cmd3.ExecuteNonQuery()
        conn3.Close()
        cmd3.Dispose()
        conn3.Dispose()
        Dim sql4 As String = "SELECT * FROM VideoView WHERE VideoID=@f20"
        Dim conn4 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        conn4.Open()
        Dim cmd4 As OleDbCommand = New OleDbCommand(sql4, conn4)
        cmd4.Parameters.AddWithValue("@f20", Request.Params("id"))
        Dim datareader2 = cmd4.ExecuteReader()
        datareader2.Read()
        If Not IsDBNull(datareader2("CountOfID")) Then lblTotalViews.Text = datareader2("CountOfID")
        imgLike.Visible = False
        Dim sql As String = "SELECT * FROM Video_Likes WHERE UserName = @f1 AND VideoID = @f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", Request.Params("id"))
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        If dr.HasRows Then
            imgLike.Visible = True
            ImageButton1.Visible = False
            'btnLike.Visible = False
        Else
            imgLike.Visible = False
            ImageButton1.Visible = True
            'btnLike.Visible = True
        End If
        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Dim sql2 As String = "SELECT * FROM VideoLikesQuery WHERE VideoID=@f20"
        Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        conn2.Open()
        Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn2)
        cmd2.Parameters.AddWithValue("@f20", Request.Params("id"))
        Dim datareader = cmd2.ExecuteReader()
        datareader.Read()
        If Not IsDBNull(datareader("CountOfID")) Then lblTotalCounts.Text = datareader("CountOfID")
    End Sub

    Protected Sub btnAddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddComment.Click

        If txtAddVideoComment.Text = "" Then
            MsgBox("Please Enter a Comment")
        Else
            Dim sql As String = "INSERT INTO Video_Comments (CommentBody,DateUploaded,TimeUploaded,UserName,VideoID) VALUES (@f1,@f2,@f3,@f4,@f5)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", txtAddVideoComment.Text)
            cmd.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
            cmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))
            cmd.Parameters.AddWithValue("@f4", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f5", Request.QueryString("id"))
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            Response.Redirect("VideosPlayComments.aspx?id=" & Request.QueryString("id"))
        End If

    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ImageButton1.Click


        'imgLike.Visible = True
        ImageButton1.Visible = False
        Dim sql As String = "INSERT INTO Video_Likes (VideoID,UserName) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", Request.Params("id"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("VideosPlayComments.aspx?id=" & Request.QueryString("id"))
    End Sub
End Class